package org.lzuploader.controller;

import java.io.File;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lzuploader.model.Config;

/** This class is used to get the list of current accounts on the user's computer.
 * It uses a {@link Config} instance in order to get the World of Warcraft folder.
 * 
 * @author Emmanuel Cron
 *
 */
public final class AccountsAccessBean
{
    /** Logger of this class. */
    private static Log log = LogFactory.getLog(AccountsAccessBean.class);
    
    /** Instance of the config of the program. */
    private static Config config = Config.getInstance();
    
    /** User should not instantiate this class. */
    private AccountsAccessBean()
    {
        throw new UnsupportedOperationException();
    }
    
    /** Reads the WTF folder and gets the accounts list.
     * You should cache the results of this function if you do not need an update
     * to be done between two uses.
     * @return A list of account names.
     */
    public static Set<String> getAccountsList()
    {
        log.debug("Getting accounts list.");
        
        final Set<String> accounts = new TreeSet<String>();
        final String wowPath = config.getWowPath();
        
        if (wowPath == null || wowPath.equals(""))
        {
            log.trace("WoW path does not exist, returning empty list.");
            
            return accounts;
        }
        
        final File wtf = new File(wowPath +  "WTF");
        
        if (!wtf.exists())
        {
            log.trace("WTF does not exist, returning empty list.");
            
            return accounts;
        }
        
        final File account = new File(wtf.getAbsolutePath() + File.separatorChar + "Account");
        
        if (!account.exists())
        {
            log.trace("WTF" + File.separatorChar + "Account folder does not exist, returning empty list.");
            
            return accounts;
        }
        
        log.trace("Reading folder: " + account.getAbsolutePath());
        
        for (String filePath : account.list())
        {
            final File file = new File(account.getAbsolutePath() + File.separatorChar + filePath);
            
            if (file.isDirectory())
            {
                log.trace("Found folder: " + account.getAbsolutePath() + File.separatorChar + file.getName());
                
                accounts.add(file.getName());
            }
        }
        
        return accounts;
    }
    
    /** Gets the size of the file to send from an account.
     * @param accountName Account to look file into
     * @return int Size of the file, or <code>0</code> if not found
     */
    public static int getFileSize(final String accountName)
    {
        log.debug("Getting LZProfiler.lua file size for account: " + accountName);
        
        final File file = getFile(accountName);
        
        if (file == null)
        {
            log.trace("File is null, returning 0.");
            
            return 0;
        }
        else
        {
            log.trace("Returning size: " + (int) file.length());
            
            return (int) file.length();
        }
    }
    
    /** Returns the LZProfiler.lua file of an account.
     * 
     * @param accountName The account name where to search for the file
     * @return A {@link File} object, or <code>null</code> if not found
     */
    public static File getFile(final String accountName)
    {
        log.debug("Getting LZProfiler.lua file object for account: " + accountName);
        
        final String wowPath = config.getWowPath();
        
        if (wowPath == null)
        {
            log.trace("The World of Warcraft path is null, returning null.");
            
            return null;
        }
        
        final StringBuffer path = new StringBuffer(wowPath);
        
        path.append("WTF");
        path.append(File.separatorChar);
        path.append("Account");
        path.append(File.separatorChar);
        path.append(accountName);
        path.append(File.separatorChar);
        path.append("SavedVariables");
        path.append(File.separatorChar);
        path.append("LZProfiler.lua");
        
        log.trace("Path of the file: " + path.toString());
        
        final File file = new File(path.toString());
        
        if (file.exists() && file.isFile())
        {
            log.trace("Returning file: " + file.toString());
            
            return file;
        }
        else
        {
            log.trace("File could not be found or file is not a file. Returning null.");
            
            return null;
        }
    }
}
